package com.example.studentregistration.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

import com.example.studentregistration.pojo.Reporting;
import com.example.studentregistration.service.ReportingService;
import com.example.studentregistration.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api")
public class ReportingController {

    @Autowired
    private ReportingService reportingService;

    /* 新增报到记录 */
    @PostMapping("/reporting")
    public Result<?> insert(@RequestBody Reporting reporting) {

        return reportingService.insertReporting(reporting);
    }

    /* 修改报到信息 */
    @PutMapping("/reporting")
    public Result<?> update(@RequestBody Reporting reporting) {
        return reportingService.updateReporting(reporting);
    }

    /* 删除报到记录 */
    @DeleteMapping("/reporting/{id}")
    public Result<?> delete(@PathVariable Long id) {
        return reportingService.deleteReporting(id);
    }

    /* 分页 + 条件查询 */
    @GetMapping("/reporting")
    public Result<?> page(@RequestParam(defaultValue = "1") Integer currentPage,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          @RequestParam(required = false) String status,
                          @RequestParam(required = false) String name,
                          @RequestParam(required = false) String studentNo,
                          @RequestParam(required = false) String className,
                          @RequestParam(required = false) String buildingName,
                          @RequestParam(required = false) Integer isPaid) {
        Page<Reporting> page = reportingService.queryReportingPage(currentPage, pageSize, status, name, studentNo, className, buildingName, isPaid);
        PageInfo<Reporting> info = new PageInfo<>(page);
        return Result.success(info);
    }

    /* 根据 ID 查单条详情 */
    @GetMapping("/reporting/{id}")
    public Result<Reporting> get(@PathVariable Long id) {
        return reportingService.queryReportingById(id);
    }

    /* 批量删除 */
    @DeleteMapping("/reporting/batch")
    public Result<?> batchDelete(@RequestBody List<Long> ids) {
        // 循环删除即可，如需批量 SQL 可自行扩展
        ids.forEach(reportingService::deleteReporting);
        return Result.success("批量删除成功");
    }

    /* 导出报到数据 */
    @GetMapping("/reporting/export")
    public void exportReporting(HttpServletResponse response) throws IOException {
        reportingService.exportReportingExcel(response);
    }

    // 导入（与 importClazz 同风格）
    @PostMapping("/reporting/import")
    public Result<?> importReporting(@RequestParam("file") MultipartFile file) throws IOException {
        InputStream is = file.getInputStream();
        ExcelReader reader = ExcelUtil.getReader(is);
        List<Reporting> list = reader.readAll(Reporting.class);   // 直接映射

        try {
            list.forEach(r -> {
                r.setCreateTime(new Date());
                r.setUpdateTime(new Date());
                reportingService.insertReporting(r);
            });
        } catch (Exception e) {
            return Result.fail("导入失败：" + e.getMessage());
        }
        return Result.success("报到数据导入成功");
    }
}
